package test;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

public class FindConfuse {

    private static final String FILE_PARENT = "D:\\android\\project\\workconfuse\\";
    private static final int LENGTH = 15;//混淆字段的长度
    private static boolean FULLPATH = true;//是否打印路径全名
    private static String file = FILE_PARENT + "trueno_confuse";//根据不同的项目，修改不同的目录

    public static void main(String[] args) throws Exception {
        findConfuse(new File(file));
    }

    private static void findConfuse(File file) throws Exception {
        if (file.isDirectory()) {
            String fileName = file.getName();
            if (!fileName.equals("build") && !fileName.equals(".git") &&
                    !fileName.equals(".idea") && !fileName.equals(".gradle"))
                for (File f : file.listFiles()) {
                    findConfuse(f);
                }
        } else {
            String content = readFileOnLine(file.getPath());
            if (content != null && !content.isEmpty())
                System.out.println(content);
        }
    }

    public static String readFileOnLine(String filePath) throws Exception {//输入文件路径
        if (filePath.contains(".png") || filePath.contains(".webp")
                || filePath.contains(".txt") || filePath.contains("app-release.aab"))
            return null;
        FileInputStream fis = new FileInputStream(filePath);//打开文件输入流
        StringBuffer sBuffer = new StringBuffer();
        DataInputStream dataIO = new DataInputStream(fis);//读取文件数据流
        String strLine = null;
        int line = 0;
        while (++line > 0 && (strLine = dataIO.readLine()) != null) {
            if (filter(strLine)) {
                if (FULLPATH)
                    sBuffer.append(padding(100, filePath.substring(file.length() + 1) + ":" + line) + strLine.trim() + "\n");
                else
                    sBuffer.append(padding(30, filePath.substring(filePath.lastIndexOf("\\") + 1) + ":" + line) + strLine.trim() + "\n");
            }
        }
        return sBuffer.toString();
    }


    private static String padding(int length, String tag) {
        if (tag.length() >= length)
            return tag;
        StringBuilder stringBuilder = new StringBuilder(tag);
        for (int i = tag.length(); i < length; i++) {
            stringBuilder.append("-");
        }
        return stringBuilder.toString();
    }


    private static boolean filter(String str) {
        if (str == null || str.isEmpty() || str.length() < LENGTH)
            return false;

        //48-57  65-90  97-122
        int count = 0;
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (ch >= 48 && ch <= 57 || ch >= 97 && ch <= 122) {
                count++;
            } else {
                if (count == LENGTH && !ignore(str, str.substring(i - LENGTH, i))) {
                    return true;
                }
                count = 0;
            }
        }
        return false;
    }

    private static boolean ignore(String str, String confuse) {
        if (str.contains("R.layout." + confuse))
            return true;
        if (str.contains("R.string." + confuse))
            return true;
        if (str.contains("R.id." + confuse))
            return true;
        if (str.contains("R.mipmap." + confuse))
            return true;
        if (str.contains("R.color." + confuse))
            return true;
        if (str.contains("R.styleable." + confuse))
            return true;
        if (str.contains("R.drawable." + confuse))
            return true;
        if (str.contains("R.array." + confuse))
            return true;
        if (str.contains("R.dimen." + confuse))
            return true;
        if (str.contains(" AttributeSet " + confuse))
            return true;
        if (str.contains("super(context, " + confuse + ");"))
            return true;
        if (str.contains("@color/" + confuse))
            return true;
        if (str.contains("@dimen/" + confuse))
            return true;
        if (str.contains("@drawable/" + confuse))
            return true;
        if (str.contains("@string/" + confuse))
            return true;
        if (str.contains("@mipmap/" + confuse))
            return true;
        if (str.contains("@menu/" + confuse))
            return true;
        if (str.contains("@layout/" + confuse))
            return true;
        if (str.contains("=\"@id/" + confuse))
            return true;
        if (str.contains("android:id=\"@+id/" + confuse))
            return true;
        if (str.contains("<string name=\"" + confuse))
            return true;
        if (str.contains("<dimen name=\"" + confuse))
            return true;
        if (str.contains("<color name=\"" + confuse))
            return true;
        if (str.contains(".obtainStyledAttributes(" + confuse))
            return true;
        if (str.contains("this(context, " + confuse))
            return true;
        if (str.contains("init(context, " + confuse))
            return true;
        if (str.contains("super(context, " + confuse))
            return true;
        if (str.contains("R.raw." + confuse))
            return true;
        if (str.contains("TextView " + confuse + ";"))
            return true;
        if (str.contains("ImageView " + confuse + ";"))
            return true;
        if (str.contains("public void " + confuse + "(View view) {"))
            return true;
        if (str.contains("<style name=\"" + confuse))
            return true;
        if (str.contains("<string-array name=\"" + confuse))
            return true;
        if (str.contains("android:resource=\"@xml/" + confuse))
            return true;
        if (str.contains(confuse + ".set("))
            return true;
        if (str.contains(confuse + ".setText("))
            return true;
        if (str.contains(confuse + ".setVisibility(View."))
            return true;
        if (str.contains(confuse + " = new Observable"))
            return true;
        if (str.contains("android:onClick=\"@{"))
            return true;
        if (str.contains("android:text=\"@{"))
            return true;
        if (confuse.equals("installreferrer"))
            return true;
        if (confuse.equals("identificadores"))
            return true;
        if (confuse.equals("transformations"))
            return true;
        if (confuse.equals("actualizaciones"))
            return true;
        return false;
    }
}